package com.openlogic.common.logic.func.aviator;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.googlecode.aviator.lexer.token.OperatorType;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorBoolean;
import com.googlecode.aviator.runtime.type.AviatorObject;

/**
 * @ClassName: CustomNotContainOp
 * @Description: 包含操作符 TODO：目前aviator不支持自定义运算符，只能覆盖原有运算符，需要官方支持，后期需维护
 * @author weiyongpeng
 * @date 2024年12月25日 下午4:55:15
 */
public class CustomNotContainOp extends AbstractFunction {

	private static final long serialVersionUID = 1L;

	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2) {
		String leftValue = arg1.getValue(env).toString().replaceAll("\"", "");
		String rightValue = arg2.getValue(env).toString().replaceAll("\"", "");
		JSONArray leftCollection = null;
		JSONArray rightCollection = null;
		try {
			if (arg1.getValue(env) instanceof Collection) {
				leftCollection = JSONArray.parseArray(JSONObject.toJSONString(arg1.getValue(env)));
			} else if (arg2.getValue(env) instanceof Map) {
				JSONObject object = JSONObject.parseObject(JSON.toJSONString(arg2.getValue(env)));
				rightCollection = new JSONArray();
				rightCollection.add(object);
			} else {
				leftCollection = JSON.parseArray(leftValue);
			}
		} catch (Exception e) {
			leftCollection = JSON.parseArray(JSON.toJSONString(Arrays.asList(leftValue)));
		}
		try {
			if (arg2.getValue(env) instanceof Collection) {
				rightCollection = JSONArray.parseArray(JSONObject.toJSONString(arg2.getValue(env)));
			} else if (arg2.getValue(env) instanceof Map) {
				JSONObject object = JSONObject.parseObject(JSON.toJSONString(arg2.getValue(env)));
				rightCollection = new JSONArray();
				rightCollection.add(object);
			} else {
				rightCollection = JSON.parseArray(rightValue);
			}

		} catch (Exception e) {
			rightCollection = JSON.parseArray(JSON.toJSONString(Arrays.asList(rightValue)));
		}
		Set<Object> leftSet = convertToNumericSet(leftCollection);
		Set<Object> rightSet = convertToNumericSet(rightCollection);
		boolean disjoint = Collections.disjoint(leftSet, rightSet);
		return AviatorBoolean.valueOf(disjoint);
	}

	@Override
	public String getName() {
		return OperatorType.BIT_AND.getToken();
	}

	/**
	 * 描述：转数字函数
	 * 
	 * @Title: convertToNumericSet
	 * @param leftCollection
	 * @return
	 * @author weiyongpeng
	 * @date 2024年12月25日 下午4:56:26
	 */
	private static Set<Object> convertToNumericSet(Collection<?> leftCollection) {
		Set<Object> numericSet = new HashSet<>();
		for (Object obj : leftCollection) {
			if (obj instanceof Number) {
				numericSet.add(((Number) obj).doubleValue());
			} else if (obj instanceof String) {
				try {
					numericSet.add(Double.parseDouble((String) obj));
				} catch (NumberFormatException e) {
					numericSet.add(obj);
				}
			} else {
				numericSet.add(obj);
			}
		}
		return numericSet;
	}
}
